//
//  ZqlPictureView.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/23.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZqlPictureView: UICollectionView {
    
    
    //配图的地址数组
    var imageUrls:[NSURL]?{
        didSet{
            //计算配图大小
            let picSize = calculationPictureView()
            //更新大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(picSize)
            }
            //更新测试label的文案
            testLable.text = "\(imageUrls?.count ?? 0)"
            //刷新界面
            reloadData()
        }
    }

    
    let itemWidth = (ScreenWidth - cellMargin * 2 - 2 * PictureCellMargin) / 3

    
    //collectionView必须设置流水布局
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //实例化流水布局
        let layOut = UICollectionViewFlowLayout()
        layOut.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layOut.minimumInteritemSpacing = PictureCellMargin
        layOut.minimumLineSpacing = PictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layOut)
        
        //注册cell
        self.registerClass(pictureCell.self, forCellWithReuseIdentifier: "cell")
        //设置数据源
        self.dataSource = self
        //关闭scrollView的点击快速回到顶部的属性
        self.scrollsToTop = false
        //设置背景色
        self.backgroundColor = UIColor.whiteColor()
        //添加测试lable
        self.addSubview(testLable)
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加测试lable
    private lazy var testLable:UILabel = UILabel(text: "", fontSize: 40, textColor: UIColor.redColor())
    
    private func calculationPictureView() -> CGSize{
        //获取图片数量
        let imageCount = imageUrls?.count ?? 0
        if imageCount == 0{
            return CGSizeZero
        }
        //获取流水布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        if imageCount == 1{
            
            let imageSize = CGSize(width: 150, height: 200)
            layout.itemSize = imageSize
            return imageSize
        }
        
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        if imageCount == 4{
            let w = itemWidth * 2 + PictureCellMargin
            return CGSize(width: w, height: w)
        }
        //1.计算行数
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = itemWidth * 3 + PictureCellMargin * 2
        let h = itemWidth * row + (row - 1) * PictureCellMargin
        return CGSizeMake(w, h)
        
    }
  
}


//实现数据源方法
extension ZqlPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! pictureCell
        cell.imageUrl = imageUrls![indexPath.item]
        return cell
    }
}


private class pictureCell:UICollectionViewCell{
    
    //图片属性
    var imageUrl:NSURL?{
        didSet{
            iconView.sd_setImageWithURL(imageUrl)
        }
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.9, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
   //懒加载视图
    private lazy var iconView:UIImageView = {
        let iv = UIImageView()
        
        //部分缩放视图
        iv.contentMode = .ScaleAspectFill
        
        //手动裁剪
        iv.clipsToBounds = true
        return iv
    }()
    
    
    
    
    
}

